Page({
    data: {
        list:null, // 题目列表
        currentPage : 0, // 当前Page
        totalPage : 0, // 总数
        first : true, // Page是否为first
        last : false, // Page是否为last
        current: '', //记录点击内容
        correctAnswer:'', //当前正确答案
        showAnswer: false, //展示答案
        id : 0, //用户id
    },
    //处理点击选项函数
    handleChoiceChange({ detail = {} }) {
        this.setData({
            current: detail.value,
            showAnswer:true
        });
        var app = getApp();
        var url = app.globalData.global_url;
        //上传用户选项
        wx.request({
            url: url+'/exercise/',//'http://localhost:8000/exercise/',
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded' // 默认值
            },
            data:{
                userChosen : detail.value.split('、')[1],
                exerciseAnswer : this.data.correctAnswer,
                exerciseContent : this.data.list[this.data.currentPage].exerciseContent,
                id : this.data.id
            },
            success: function (response){
                if (response.statusCode == 500){
                    wx.showToast({
                        title: '记录答题历史失败',
                        icon: 'error',
                        duration: 2000
                    })
                }
            },
            fail: function (response){
                wx.showToast({
                    title: '服务器错误',
                    icon: 'error',
                    duration: 2000
                })
            }
        })
    },
    //上一页函数
    lastPage(){
        // 判断是否到第一页，是否隐藏上一页按钮
        if (this.data.currentPage == 1) {
            this.setData({
                correctAnswer: this.data.list[this.data.currentPage - 1].answer,
                currentPage: this.data.currentPage - 1,
                first: true,
                last: false,
                current : '',
                showAnswer: false
            });
        } else {
            this.setData({
                correctAnswer: this.data.list[this.data.currentPage - 1].answer,
                currentPage: this.data.currentPage - 1,
                first: false,
                last: false,
                current: '',
                showAnswer: false
            });
        }
    },
    //下一页函数
    nextPage(){
        // 判断是否到最后页，是否隐藏下一页按钮
        if (this.data.currentPage == this.data.totalPage-2) {
            this.setData({
                correctAnswer: this.data.list[this.data.currentPage + 1].answer,
                currentPage: this.data.currentPage + 1,
                first: false,
                last: true,
                current:'',
                showAnswer: false
            });
        } else {
            this.setData({
                correctAnswer: this.data.list[this.data.currentPage + 1].answer,
                currentPage: this.data.currentPage + 1,
                first: false,
                last: false,
                current:'',
                showAnswer: false
            });
        }
    },

    // onLoad函数
    onLoad: function (options) {
        var that = this;
        var app = getApp();
        var url = app.globalData.global_url;
        wx.login({
            success (res) {
                if (res.code) {
                    //发起网络请求,查询并保存用户id
                    wx.request({
                        url: url+'/user/getOpenId',//'http://localhost:8000/user/getOpenId',
                        data: {
                          code: res.code
                        },
                        success: function (response) {
                            if (response.statusCode == 200){
                                that.setData({
                                    id : response.data
                                })
                            }
                            else{
                                wx.showToast({
                                    title: '获取openid失败',
                                    icon: 'error',
                                    duration: 2000
                                })
                            }
                        },
                        fail:function(res){
                            wx.showToast({
                                title: '服务器错误',
                                icon: 'error',
                                duration: 2000
                            })
                        }
                    })
                }
                else {
                    console.log('登录失败！' + res.errMsg)
                }
            }
        })
        //查询练习题内容
        wx.request({
            url: url+'/exercise?type=exercise',//'http://localhost:8000/exercise?type=exercise',
            method: 'GET',
            header: {
            'content-type': 'application/json' // 默认值
            },
            // 初始化题目列表等数据
            success:res=>{
                if(res.statusCode == 200){
                    that.setData({
                        list: res.data.exerciseList,
                        totalPage : res.data.size,
                        correctAnswer: res.data.exerciseList[0].answer,
                        loading: false
                    })
                }
                else{
                    wx.showToast({
                        title: '获取题目列表失败',
                        icon: 'error',
                        duration: 2000
                    })
                }
            },
            fail:function(res){
                wx.showToast({
                    title: '服务器错误',
                    icon: 'error',
                    duration: 2000
                })
            }
        })
    },

});